package com.sjtu.rental.services;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.sjtu.rental.common.PaginationSupport;

@Component
public class GenericDao extends HibernateDaoSupport {

	@SuppressWarnings("unchecked")
	public PaginationSupport findByCriteria(DetachedCriteria crit, int page) {
		crit.setProjection(Projections.rowCount());
		List<Object> counts = getHibernateTemplate().findByCriteria(crit);
		PaginationSupport p = new PaginationSupport((Integer) counts.get(0));
		p.setPage(page);
		crit.setProjection(null);
		p.setItems(getHibernateTemplate().findByCriteria(crit,
				p.getStartIndex(), p.getPageSize()));
		return p;
	}
}
